package com.yss.reportworld.controller;

import com.yss.common.core.constant.Constants;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.redis.service.RedisService;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.common.security.utils.DictUtils;
import com.yss.reportworld.domain.arrwrap.TcRepPara;
import com.yss.reportworld.enums.ReportWorldEnum;
import com.yss.reportworld.service.*;
import com.yss.system.api.domain.SysDictData;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 报表部署包Controller
 *
 * @author yss
 * @date 2023-03-29
 */
@RestController
@RequestMapping("/arrWrap")
public class ArrWrapController extends BaseController {
    @Autowired
    private IArrWrapService iArrWrapService;
    @Autowired
    private ITpGlExhibDatadictService tpGlExhibDatadictService;
    @Autowired
    private ITcRepParaService tcRepParaService;
    @Autowired
    private ISqlScriptService sqlScriptService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private RedisService redisService;

    /**
     * 初始化加载报表世界监管规则
     *
     * @return
     */
    @RequiresPermissions("reportworld:arrwrap:list")
    @Log(title = "报表部署包", businessType = BusinessType.OTHER)
    @PutMapping("/init")
    public AjaxResult init() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("regulationRuleList", tpGlExhibDatadictService.queryRegulationOrg());
        // 获取备份的表
        List<SysDictData> sysDictDataList = sqlScriptService.getReductionVersion(false);
        ajax.put("hisVersions", sysDictDataList);
        ajax.put("customerTree", customerService.selectCustomerTreeList());
        return ajax;
    }

    /**
     * 查询报表基本信息表列表
     */
    @RequiresPermissions("reportworld:arrwrap:list")
    @GetMapping("/list")
    public TableDataInfo list(TcRepPara tcRepPara) {
        startPage();
        List<TcRepPara> list = tcRepParaService.selectTcRepParaList(tcRepPara);
        return getDataTable(list);
    }

    /**
     * 导出zip文件，提供给升级工具
     *
     * @return
     * @throws Exception
     */
    @RequiresPermissions("reportworld:arrwrap:arrwrapszip")
    @Log(title = "报表部署包", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/downArrWrapsZip")
    public void downUpgradeToolsZip(HttpServletResponse response, TcRepPara tcRepPara) throws Exception {
        if (StringUtils.isNull(tcRepPara)) {
            throw new BusinessException("没有获取到要导出的报表部署包信息！");
        }
        if (null == tcRepPara.getExportType()) {
            throw new BusinessException("请选择要导出方式！");
        }
        if (1 == tcRepPara.getExportType()) {
            if (StringUtils.isEmpty(tcRepPara.getRegulationRule())) {//导出方式是规则分类
                throw new BusinessException("请选择要导出的监管规则！");
            }
        } else {
            if (StringUtils.isEmpty(tcRepPara.getIds())) {
                throw new BusinessException("请选择要导出的具体报表！");
            }
        }
        if (StringUtils.isEmpty(tcRepPara.getDataBaseType())) {
            throw new BusinessException("请选择要导出的报表的数据库类型！");
        }
        byte[] data = iArrWrapService.downArrWrapsZip(tcRepPara);
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }


    /**
     * 读取报表部署报表
     */
    @RequiresPermissions("reportworld:arrwrap:readZip")
    @Log(title = "报表部署包", businessType = BusinessType.IMPORT)
    @PostMapping("/readZip")
    public AjaxResult readZip(MultipartFile file, String hisVersion, HttpServletResponse response) throws Exception {
        String sql = iArrWrapService.getArrWrapsSql(file, hisVersion);
        redisService.setCacheObject("ARRWRAPSQL", sql);
        return success(sql);
    }

    /**
     * 导出SQL文件
     */
    @RequiresPermissions("reportworld:arrwrap:readZip")
    @Log(title = "报表部署包", businessType = BusinessType.EXPORT)
    @PostMapping("/handleDownSql")
    public void handleDownSql(HttpServletResponse response) throws IOException {
        if (redisService.hasKey("ARRWRAPSQL")) {
            String sql = redisService.getCacheObject("ARRWRAPSQL");
            redisService.deleteObject("ARRWRAPSQL");
            IOUtils.write(sql, response.getOutputStream(), Constants.UTF8);
        }
    }

}
